package go.tetris.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventPreview;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class Tetris implements EntryPoint, EventPreview {
	//private Tilemap tilemap = new Tilemap(10, 20);
	private TetrisPanel tetrispanel = new TetrisPanel(20, 22);

	public static void gameOverEx() {
		// dialog uusgeh
		final DialogBox dialogBox = new DialogBox();
		final String gameOverText = "Хожигдлоо. <a href='http://www.google.com/"
				+ "search?q=lost+the+game'>The Game</a>";
		dialogBox.setHTML(gameOverText);
		dialogBox.setAnimationEnabled(true);
		HTML html = new HTML("<u>Удирдах</u><br/>"
				+ "<b>Баруун, зүүн, доошоо сумаар</b> удирдана<br/>"
				+ "<b>Дээшээ сумаар</b>: Дүрсийг эргүүлнэ.<br/>" + "<b>space</b>: drop");
		Button button = new Button("New Game");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setWidth("100%");
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		dialogVPanel.add(html);
		dialogVPanel.add(button);

		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				dialogBox.hide();
				newGame();
			}
		});


		dialogBox.setWidget(dialogVPanel);
		dialogBox.center();
		dialogBox.show();
	}

	public static void gameOver() {
		NewGameDialog dialog = new NewGameDialog();
		dialog.show();
	}

	public static void newGame() {
		TetrisPanel.clear();
		CubeBlock.newTetro();
	}

	public boolean onEventPreview(Event event) {
		if (event.getTypeInt() == Event.ONKEYPRESS) {
			int keyCode = event.getKeyCode();
			if (keyCode == KeyboardListener.KEY_RIGHT) {
				CubeBlock.moveRight();
			} else if (keyCode == KeyboardListener.KEY_LEFT) {
				CubeBlock.moveLeft();
			} else if (keyCode == KeyboardListener.KEY_DOWN) {
				CubeBlock.moveDown();
			} else if (keyCode == KeyboardListener.KEY_UP) {
				CubeBlock.rotate();
			} else if (keyCode == KeyboardListener.KEY_END || keyCode == ' ') {
				CubeBlock.moveDrop();
			} else if (keyCode == KeyboardListener.KEY_DELETE) {
				TetrisPanel.clear();
			}
		}
		return true;
	}

	public void onModuleLoad() {
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		vPanel.add(tetrispanel);
		vPanel.add(Onoo.getPanel());
		
		//Tile tur  = new Tile();
		//vPanel.add(tur);
		//tur.draw(0);
		RootPanel.get("tTitle").addStyleName("tTitle");
		
		RootPanel.get().add(vPanel);
		DOM.addEventPreview(this);
		// Tetromino.newTetro();
		gameOver();
	}
}
